package com.jiulan.hearthearing.ui.my.index;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseVPFragment;
import com.jiulan.hearthearing.common.UserData;
import com.jiulan.hearthearing.ui.cart.activity.CartActivity;
import com.jiulan.hearthearing.ui.menus.MenuAdapter;
import com.jiulan.hearthearing.ui.menus.MenuModel;
import com.jiulan.hearthearing.ui.my.activity.CoursesPurchasedActivity;
import com.jiulan.hearthearing.ui.my.activity.MyCollectionActivity;
import com.jiulan.hearthearing.ui.my.activity.MyCourseActivity;
import com.jiulan.hearthearing.ui.my.activity.PersonInfoActivity;
import com.jiulan.hearthearing.ui.my.activity.SettingActivity;
import com.jiulan.hearthearing.utils.StringUtils;
import com.shehuan.niv.NiceImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的
 */
public class UserCenterIndex extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private MenuAdapter typeAdapter;
    private List<MenuModel> typeModels;
    private MenuAdapter iconAdapter;
    private List<MenuModel> iconModels;

    private View view;
    private LinearLayout llPersonalInfo;
    private NiceImageView nivHeadPhoto;
    private TextView tvNickname;
    private TextView tvInfo;
    private RecyclerView rcvType;
    private RecyclerView rcvIcon;
    private RelativeLayout rlSet;

    public static UserCenterIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
        UserCenterIndex fl = new UserCenterIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_user_center_index, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    private void initView() {
        llPersonalInfo = view.findViewById(R.id.llPersonalInfo);
        llPersonalInfo.setOnClickListener(this);
        nivHeadPhoto = view.findViewById(R.id.nivHeadPhoto);
        tvNickname = view.findViewById(R.id.tvNickname);
        tvInfo = view.findViewById(R.id.tvInfo);
        rcvType = view.findViewById(R.id.rcvType);
        rcvIcon = view.findViewById(R.id.rcvIcon);
        rlSet = view.findViewById(R.id.rlSet);
        rlSet.setOnClickListener(this);
        rcvType.setLayoutManager(new GridLayoutManager(context, 4));
        rcvIcon.setLayoutManager(new GridLayoutManager(context, 4));
        setTypes();
        setIcons();
    }

    private void setTypes() {
        typeModels = new ArrayList<>();
        typeModels.add(new MenuModel(R.mipmap.my_kecheng, "我的课程"));
        typeModels.add(new MenuModel(R.mipmap.my_dabian_apply, "申请答辩"));
        typeModels.add(new MenuModel(R.mipmap.my_yuyuejianxi, "预约见习"));
        typeModels.add(new MenuModel(R.mipmap.my_gangweizhengshu, "岗位证书"));
        typeAdapter = new MenuAdapter(typeModels);
        rcvType.setAdapter(typeAdapter);
        typeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (typeModels.get(position).getActiveName()) {
                    case "我的课程":
                        MyCourseActivity.actionStart(context);
                        break;
                    case "申请答辩":
                        Toast.makeText(context, "申请答辩", Toast.LENGTH_SHORT).show();
                        break;
                    case "预约见习":
                        Toast.makeText(context, "预约见习", Toast.LENGTH_SHORT).show();
                        break;
                    case "岗位证书":
                        Toast.makeText(context, "岗位证书", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    private void setIcons() {
        iconModels = new ArrayList<>();
        iconModels.add(new MenuModel(R.mipmap.my_xuexishuju, "学习数据"));
        iconModels.add(new MenuModel(R.mipmap.my_download, "下载视频"));
        iconModels.add(new MenuModel(R.mipmap.my_collections, "我的收藏"));
        iconModels.add(new MenuModel(R.mipmap.my_yigoukecheng, "已购课程"));
        iconModels.add(new MenuModel(R.mipmap.my_account_money, "账户余额"));
        iconModels.add(new MenuModel(R.mipmap.my_cart, "购物车"));
        iconModels.add(new MenuModel(R.mipmap.my_coupon, "优惠券"));
        iconModels.add(new MenuModel(R.mipmap.my_guahaoyuyue, "挂号预约"));
        iconAdapter = new MenuAdapter(iconModels);
        rcvIcon.setAdapter(iconAdapter);
        iconAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (iconModels.get(position).getActiveName()) {
                    case "学习数据":
                        Toast.makeText(context, "学习数据", Toast.LENGTH_SHORT).show();
                        break;
                    case "下载视频":
                        Toast.makeText(context, "下载视频", Toast.LENGTH_SHORT).show();
                        break;
                    case "我的收藏":
                        MyCollectionActivity.actionStart(context);
                        break;
                    case "已购课程":
                        CoursesPurchasedActivity.actionStart(context);
                        break;
                    case "账户余额":
                        Toast.makeText(context, "账户余额", Toast.LENGTH_SHORT).show();
                        break;
                    case "购物车":
                        CartActivity.actionStart(context);
                        break;
                    case "优惠券":
                        Toast.makeText(context, "优惠券", Toast.LENGTH_SHORT).show();
                        break;
                    case "挂号预约":
                        Toast.makeText(context, "挂号预约", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible){
            setUserInfo();
        }
    }

    private void setUserInfo() {
        if (!StringUtils.isEmpty(UserData.create(context).get(UserData.AVATAR))) {
            Glide.with(context).load(UserData.create(context).get(UserData.AVATAR)).into(nivHeadPhoto);
        } else {
            nivHeadPhoto.setImageResource(R.mipmap.my_head_photo);
        }
        tvNickname.setText(UserData.create(context).get(UserData.NICKNAME));
        tvNickname.setText(UserData.create(context).get(UserData.NICKNAME));
        tvInfo.setText(UserData.create(context).get(UserData.PERSONSIGN));
    }

    @Override
    protected void onFragmentFirstVisible() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llPersonalInfo:
                PersonInfoActivity.actionStart(context);
                break;
            case R.id.rlSet:
                SettingActivity.actionStart(context);
                break;
        }
    }
}
